/**
 * 场境储存器
 */
class SceneManager {
	// 单例
	private static _ins: SceneManager;
	/**
	 * 单例
	 */
	public static get ins() {
		// Instance
		if (SceneManager._ins == null) {
			SceneManager._ins = new SceneManager();
		}
		return SceneManager._ins;
	}



	/**
	 * 前置
	 */
	public constructor() {
		if (!this.rootLayer) {
			this.rootLayer = new eui.UILayer;
		}
	}


	public rootLayer: eui.UILayer;	// 起始场景
	private currentScene: Scene;	// 需要显示的场景
	private pop_scene: PanelBase;	// 弹出场景层
	private _confirm;				// 询问框
	private _mg;					// 蒙板
	private _alert_last_time: number;		// 最后一次弹出时间



	/**
	 * 切换场景
	 */
	public changeScene(s: Scene) {
		if (this.currentScene) {
			this.rootLayer.removeChild(this.currentScene);
			this.currentScene = null;
		}
		s.height = CommonData.WIN_H;
		this.rootLayer.addChild(s);
		this.currentScene = s;
	}



	/**
	 * 弹出窗口
	 */
	public pushScene(s: PanelBase, p: string = "verticalCenter", pn: number = 0) {

		// 隐藏当前窗口
		this.popScene();
		if (this.pop_scene || this.pop_scene == s) {
			return;
		}
		this.pop_scene = s;

		// 移除动画
		egret.Tween.removeTweens(this.pop_scene)

		// 设置动画初始值
		this.pop_scene.scaleX = 0;
		this.pop_scene.scaleY = 0;
		this.pop_scene.alpha = 0;
		this.pop_scene[p] = [pn];
		this.pop_scene.horizontalCenter = 0;
		this.rootLayer.addChildAt(this.pop_scene, 1);

		// 动画弹出
		egret.Tween.get(this.pop_scene).to({
			"scaleX": 1,
			"scaleY": 1,
			"alpha": 1
		}, 200);
	}



	/**
	 * 关闭窗口
	 */
	public popScene() {
		if (this.pop_scene) {

			// 移除动画
			egret.Tween.removeTweens(this.pop_scene)

			// 动画关闭
			egret.Tween.get(this.pop_scene).to({
				"scaleX": 0,
				"scaleY": 0,
				"alpha": 0
			}, 160).call(() => {
				// 删除元素
				this.rootLayer.removeChild(this.pop_scene);
				this.pop_scene = null;
			});

		}
	}



	/**
	 * 加一层蒙版
	 */
	private addMb() {
		// 加一层蒙版
		var r = new eui.Rect();
		r.width = CommonData.WIN_W;
		r.height = CommonData.WIN_H;
		r.alpha = 0.1;
		this.rootLayer.addChildAt(r, 2);
		// this._mg = r;
		return r;
	}



	/**
	 * 提示信息
	 * 
	 * @param msg 提示内容
	 */
	public alert(msg: string) {
		let _msg = msg;
		if (typeof this._alert_last_time == 'undefined') {
			this._alert_last_time = 0;
		}
		if (Utils.time() * 1000 <= this._alert_last_time + 1800) {
			// 正在显示一个提示
			this._alert_last_time = this._alert_last_time + 1800;
			egret.setTimeout(function () {
				this.doalert(_msg)
			}, this, this._alert_last_time - Utils.time() * 1000)
		} else {
			// 立即显示提示
			this._alert_last_time = Utils.time() * 1000;
			this.doalert(_msg);
		}

	}
	private doalert(msg: string) {
		let r = this.addMb();
		// 弹出窗口
		var p = new AlertPanel(msg);
		this.rootLayer.addChildAt(p, 4);

		// 设置位置
		p.horizontalCenter = 0;
		p.verticalCenter = 0;

		// 定时关闭
		egret.setTimeout(function () {
			this.rootLayer.removeChild(r);
			this.rootLayer.removeChild(p);
		}, this, 1500)
	}



	/**
	 * 询问框
	 * @param msg 提示内容
	 * @param callback 回调
	 */
	public confirm(msg: string, callback: Function) {
		let r = this.addMb();
		// 弹出窗口
		var p = new ConfirmPanel(msg, callback);
		this.rootLayer.addChildAt(p, 4);

		// 设置位置
		p.horizontalCenter = 0;
		p.verticalCenter = 0;

		this._mg = r;
		this._confirm = p
	}



	/**
	 * 关闭询问框
	 */
	public confirmClose() {
		this.rootLayer.removeChild(this._mg);
		this.rootLayer.removeChild(this._confirm);
	}

}
